



/*************************** http util **************************/

function request(data={}) {
  this.$axios({
    url:'',
    data:data,
    header:{
        'Content-Type':'application/json'  //如果写成contentType会报错
    }
  })
  .then(res=>{
    console.log(res.data)
  })
  .catch(Error=>{
    console.log(Error)
  })
}

async function postJson(config={}) {
  headers = {
    'Content-Type': 'application/json'  //如果写成contentType会报错
  }
  config.method = 'post'
  config.headers = headers
  
  let response = await axios(config)
  console.log('response: ', JSON.stringify(response.data))
  console.log('status: ', response.status)
  console.log('headers: ', response.headers)
  return response
}

async function putJson(config={}) {
  headers = {
    'Content-Type': 'application/json'  //如果写成contentType会报错
  }
  config.method = 'put'
  config.headers = headers
  
  let response = await axios(config)
  console.log('response: ', JSON.stringify(response.data))
  console.log('status: ', response.status)
  console.log('headers: ', response.headers)
  return response
}

// const got = require('got');
// const axios = require("axios");
async function httpRequest(config={}) {
    console.log('>>httpRequest')
    let response = await axios(config)
    console.log('response: ', JSON.stringify(response.data))
    // baseUtil.sleep(1000)
    console.log('status: ', response.status)
    console.log('headers: ', response.headers)
    return response
  }
  
async function httpRequest2({uri, method='post', data={}, params={}, baseUrl=''}) {
  console.log('>>httpRequest')
  let config = {
    url: uri,
    method: method,
    params: params,
    data: data,
    baseUrl: baseUrl
  }
  let response = await axios(config)
  console.log('response: ', JSON.stringify(response.data))
  console.log('status: ', response.status)
  console.log('headers: ', response.headers)
  return response
}

//  GET 方法封装
const httpGet = (url, params = {}, headers={}) => {
  //let token = params.token ? params.token : getCookie('agtoken')
  /*
  let token = getCookie('agtoken')
  if (!token) {
    //debugger
    window.location.reload()
  }
  headers.agtoken = token
  */
  //console.log('headers: ', headers)
  return axios({
    method: 'get',
    url: url,
    params: params,
    headers: headers
  });
}

const http = {
  req() {
    this.$axios({
      url:'',
      data:jsons,
      header:{
          'Content-Type':'application/json'  //如果写成contentType会报错
      }
    })
    .then(res=>{
      console.log(res.data)
    })
    .catch(Error=>{
      console.log(Error)
    })
  },
  postJson: postJson,
  putJson: putJson,
  get: httpGet
}
